package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.constant.Constants;
import com.spzx.common.core.utils.ServletUtils;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Product;
import com.spzx.product.domain.ProductDetails;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.domain.SkuStock;
import com.spzx.product.mapper.ProductMapper;
import com.spzx.product.service.ProductDetailsService;
import com.spzx.product.service.ProductService;
import com.spzx.product.service.ProductSkuService;
import com.spzx.product.service.SkuStockService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Resource
    ProductDetailsService productDetailsService;
    @Resource
    SkuStockService skuStockService;
    @Resource
    ProductSkuService productSkuService;
    @Override
    public Page<Product> selectProductPage(Product product) {
        Integer pageNum = ServletUtils.getParameterToInt(Constants.PAGE_NUM, 1);
        Integer pageSize = ServletUtils.getParameterToInt(Constants.PAGE_SIZE, 3);
        Page<Product> page = new Page<>(pageNum,pageSize);
        List<Product> list = baseMapper.selectProductPage(page,product);
        page.setRecords(list);
        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveProduct(Product product) {
        //1保存product数据: sku和details都需要关联spu的id
        //设置默认参数
        product.setStatus((byte) 0);
        product.setAuditStatus((byte) 0);
        product.setCreateBy(SecurityUtils.getUsername());
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        product.setDelFlag("0");
        this.save(product);
        //获取新增的product的id值
        Long spuId = product.getId();
        //2、保存product_details:  和product是 1:1的关系
        List<String> detailsImageUrlList = product.getDetailsImageUrlList();
        if(!CollectionUtils.isEmpty(detailsImageUrlList)){
            ProductDetails productDetails = new ProductDetails();
            productDetails.setId(spuId);
            productDetails.setProductId(spuId);
            productDetails.setCreateTime(new Date());
            productDetails.setCreateBy(SecurityUtils.getUsername());

            productDetails.setImageUrls(StringUtils.join(detailsImageUrlList,","));
            productDetails.setUpdateTime(new Date());
            productDetails.setDelFlag("0");
            productDetailsService.save(productDetails);
        }
        //3、保存sku数据
        if(!CollectionUtils.isEmpty(product.getProductSkuList())){
            for (int i = 0; i < product.getProductSkuList().size(); i++) {
                ProductSku sku = product.getProductSkuList().get(i);
                //保存sku
                sku.setProductId(spuId);
                //商品码:使用了 hutool-core包中的IdUtil工具类 生成
//            sku.setSkuCode( IdUtil.getSnowflakeNextIdStr());
                sku.setSkuCode(spuId+"_"+ i);
                sku.setCreateBy(SecurityUtils.getUsername());
                //sku标题：使用spu标题+sku的spec值生成
                sku.setSkuName(product.getName()+" "+ sku.getSkuSpec());
                sku.setCreateTime(new Date());
                sku.setUpdateTime(new Date());
                sku.setStatus((byte) 0);
                sku.setDelFlag("0");
                productSkuService.save(sku);
                //保存sku的库存
                SkuStock skuStock = new SkuStock();
                skuStock.setSkuId(sku.getId());
                skuStock.setTotalNum(sku.getStockNum());//库存
                skuStock.setLockNum(0);
                skuStock.setAvailableNum(sku.getStockNum());
                skuStock.setSaleNum(0);
                skuStock.setDelFlag("0");
                skuStock.setUpdateTime(new Date());
                skuStock.setCreateTime(new Date());
                skuStockService.save(skuStock);
            }


        }


        return 1;
    }

    @Override
    public Product selectProductById(Long id) {
        //1、查询spu
        Product product = this.getById(id);
        //2、查询spu_details
        ProductDetails productDetails = productDetailsService.getById(id);
        if(productDetails!=null){
            product.setDetailsImageUrlList(
                    StringUtils.isEmpty(productDetails.getImageUrls())?null:
                            Arrays.asList(productDetails.getImageUrls().split(",")));

        }
        //3、查询sku
        List<ProductSku> productSkus = productSkuService.list(Wrappers.lambdaQuery(ProductSku.class)
                .eq(ProductSku::getProductId, id));
        if(!CollectionUtils.isEmpty(productSkus)){
            productSkus.forEach(productSku -> {
                //4、查询sku的库存
                SkuStock skuStock = skuStockService.getOne(Wrappers.lambdaQuery(SkuStock.class)
                        .eq(SkuStock::getSkuId, productSku.getId())
                        .last("limit 1"));
                productSku.setStockNum(skuStock.getTotalNum());
            });
        }
        product.setProductSkuList(productSkus);
        return product;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStatus(Long id, Integer status) {
        //更新product表的上下架状态
        this.update(Wrappers.lambdaUpdate(Product.class)
                .eq(Product::getId , id)
                .set(Product::getStatus ,status )
                .set(Product::getUpdateBy , SecurityUtils.getUsername()));
        //更新spu下sku的上下架状态： sku表
        productSkuService.update(Wrappers.lambdaUpdate(ProductSku.class)
                .eq(ProductSku::getProductId , id)
                .set(ProductSku::getStatus,status)
                .set(ProductSku::getUpdateBy , SecurityUtils.getUsername()));
        return 1;
    }
}
